#include "stdafx.h"
#include "Watch.h"
#include "WatchApp.h"
//#include "MyDetours.h"
#include "DetourManager.h"



#ifdef _MANAGED
#pragma managed(push, off)
#endif



///////////////////////////////////////////////////////////////////////////////////////////////////
// Store the HOOK handle in a shared section. Why? Because this DLL is going to be mapped into
// every process on the machine, and we need to be able to refer to this hook handle in order
// for the hook chain to work properly.
///////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////
// Yeah... globals... terrible programmer. In future installments of the botting series, we'll
// start abstracting all these away into various objects.
///////////////////////////////////////////////////////////////////////////////////////////////////

WatchApp * g_pClient = NULL;    // The one-and-only (per process) poker client object



///////////////////////////////////////////////////////////////////////////////////////////////////
// This function is a stand-in for the abstract factory mechanism used to instantiate derived
// OnlinePokerClient objects (which may live in different DLLs). Since this is just a simple
// demo, we have it hard-coded to produce a PokerTimePokerClient object.
///////////////////////////////////////////////////////////////////////////////////////////////////
WatchApp* ImplicitCreate()
{
    //if (::GetModuleHandle(_T("MPPOKER.EXE")) != NULL)
    //    return new PokerTimePokerClient();

	WatchApp *pClient = NULL;

/*    if (::GetModuleHandle(_T("POKERSTARS.EXE")) != NULL)
        pClient = NULL;
    else if (::GetModuleHandle(_T("FULLTILTPOKER.EXE")) != NULL)
        pClient = NULL;*/

	pClient = new WatchApp(_T("FULLTILTPOKER.EXE"),_T("FULLTILTPOKER.EXE"), ::GetModuleHandle(_T("FULLTILTPOKER.EXE")));
    return pClient;
}





///////////////////////////////////////////////////////////////////////////////////////////////////
// Virtually every Win32 DLL has one of these. Called when the DLL is mapped/unmapped from a
// particular process's address space. We use it to set a simple flag indicating whether the
// current process is a poker client process or not.
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved  )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
		CDetourManager::Init(hModule);
        break;

    case DLL_THREAD_ATTACH:  break;
    case DLL_THREAD_DETACH:  break;
    case DLL_PROCESS_DETACH:
		CDetourManager::Exit();
		break;
    }

    return TRUE;
}









///////////////////////////////////////////////////////////////////////////////
//  Install the global window procedure hook, causing this DLL to be mapped
//  into the address space of every process on the machine.
///////////////////////////////////////////////////////////////////////////////
bool WATCH_API InstallHook(LPCWSTR pokerClientPath, bool installCBTHook)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));
    si.cb = sizeof(si);

    // The following call loads Poker Stars with our DLL (XPokerBot.Hook.dll) attached.
    DetourCreateProcessWithDll( pokerClientPath, NULL, NULL, NULL, TRUE, 
                                CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED, NULL, NULL,
                                &si, &pi, "detoured.dll", "Watch.dll", NULL);

    // We're still going to set a Windows Hook! We do this for two reasons:
    //      - In case the call to DetourCreateProcessWithDll fails.
    //      - Because we still want to respond to window create, activate,
    //        and destroy notifications.
    //if (installCBTHook)
     //   g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, g_hInstance, 0);

    // Remember, we're creating the Poker Stars process in a suspended state. This call wakes it up.
    ResumeThread(pi.hThread);

    // No error handling for this example
    return true;
}



///////////////////////////////////////////////////////////////////////////////
//  Uninstall the global window procedure hook.
///////////////////////////////////////////////////////////////////////////////
void WATCH_API UninstallHook()
{
    /*if (g_hHook)
    {
        UnhookWindowsHookEx(g_hHook);
        g_hHook = NULL;
    }*/
}
